Lists


In [1]:
a = 5
name = "Shantnu"
print(a)
print(name)


5
Shantnu

In [2]:
names = ["Shantnu", "John", "Sally"]
print(names)


['Shantnu', 'John', 'Sally']

In [3]:
for name in names:
    print("Hello ", name)


Hello  Shantnu
Hello  John
Hello  Sally

In [14]:
names.append("Mr Smith")
print(names)


['Shantnu', 'John', 'Sally', 'Mr Smith']

In [5]:
# Ipython only! be careful
names


Out[5]:
['Shantnu', 'John', 'Sally', 'Mr Smith']

In [7]:
# Start from 0
print(names[0])
print(names[2])


Shantnu
Sally

In [9]:
# Weird!

print(names[-1])

# prints last value

print(names[-2])


Mr Smith
Sally

In [11]:
# learn to google! Remove from list
names.append("Wrong name! Please remove me")
print(names)


['Shantnu', 'John', 'Sally', 'Mr Smith', 'Wrong name! Please remove me', 'Wrong name! Please remove me']
['Shantnu', 'John', 'Sally', 'Mr Smith', 'Wrong name! Please remove me']

In [13]:
del names[-1]
print(names)


['Shantnu', 'John', 'Sally']

In [15]:
names


Out[15]:
['Shantnu', 'John', 'Sally', 'Mr Smith']

In [17]:
# Array slicing
# Add another few names. Make it 10
names.append("K")
names.append("l")
names.append("m")
names.append("n")
names


Out[17]:
['Shantnu', 'John', 'Sally', 'Mr Smith', 'K', 'l', 'm', 'n']

In [18]:
# prnt 1st five names
names[:5]


Out[18]:
['Shantnu', 'John', 'Sally', 'Mr Smith', 'K']

In [19]:
names[5:]


Out[19]:
['l', 'm', 'n']

In [20]:
# And this is where it gets interetsing

names[2:6]


Out[20]:
['Sally', 'Mr Smith', 'K', 'l']

In [26]:
# You can even do
# removes 1st and last valyues!
names[1:-1]


Out[26]:
['John', 'Sally', 'Mr Smith', 'K', 'l', 'm']

In [27]:
# Go from 2 to 6 in steps of 2
names[2:6:2]


Out[27]:
['Sally', 'K']

In [37]:
# If loop

for name in names:
    if (name == "Sally"):
        print("Found Sally!")
    elif (name == "l"):
        print("Mr T found")
    else:
        print("Unknown name")


Unknown name
Unknown name
Found Sally!
Unknown name
Unknown name
Mr T found
Unknown name
Unknown name

Dictionary


In [28]:
age_dict = {"Bob":21, "Sally":33, "L":2}
age_dict


Out[28]:
{'Bob': 21, 'L': 2, 'Sally': 33}

In [29]:
age_dict["Bob"]


Out[29]:
21

In [30]:
age_dict["Shantnu"] = 16
age_dict


Out[30]:
{'Bob': 21, 'L': 2, 'Sally': 33, 'Shantnu': 16}

List comprehension


In [34]:
# We have a list. Divide each value by 5

numbers = [5,10,15,20,25]
numbers2 = []

for n in numbers:
    numbers2.append(n / 5)
    
print(numbers)    
print(numbers2)


[5, 10, 15, 20, 25]
[1.0, 2.0, 3.0, 4.0, 5.0]

In [35]:
numbers3 = [ n / 5 for n in numbers]
print(numbers3)


[1.0, 2.0, 3.0, 4.0, 5.0]

In [36]:
import numpy as np

numbers_np = np.array(numbers)
numbers_np = numbers_np / 5

print(numbers_np)


[ 1.  2.  3.  4.  5.]

Strings


In [39]:
# Strings are like lists!

name = "Shantnu"
for n in name:
    print(n)


S
h
a
n
t
n
u

In [40]:
sirname = "Tiwari"
print(name + sirname)


ShantnuTiwari

File I/o

Save the following into a file called names.txt

Shantnu, John, Sally, Mr Smith


In [42]:
with open("names.txt", "r") as f:
    names_file = f.read()
print(names_file)


Shantnu, John, Sally, Mr Smith

In [44]:
names_file_separate = names_file.split(",")
names_file_separate


Out[44]:
['Shantnu', ' John', ' Sally', ' Mr Smith']

In [54]:
names_file_separate = [name.replace(" ", "") for name in names_file_separate]
names_file_separate


Out[54]:
['Shantnu', 'John', 'Sally', 'MrSmith']

Functions


In [55]:
def hello(name):
    print("Hello ", name)
    
hello("Shantnu")


Hello  Shantnu

In [56]:
def hello2(name):
    return "Hello " + name
    
print(hello2("Shantnu")  )


Hello Shantnu

In [57]:
def hello_age(name, age):
    name_hello = "Hello " + name
    age = age  + 10
    return name_hello, age

name_hello, age = hello_age("Shan", 16)
print("Hello {} in 10 years you will be {} years old! Shocking".format(name_hello, age ))


Hello Hello Shan in 10 years you will be 26 years old! Shocking

In [ ]: